package org.mysfit.ctp4j.infra.utils;

import org.mysfit.ctp4j.api.vo.BonsVO;
import org.mysfit.ctp4j.api.vo.QuoteVO;
import org.mysfit.ctp4j.api.vo.ReqVO;
import org.mysfit.ctp4j.api.vo.TradeVO;
import org.mysfit.ctp4j.infra.pojo.CtpUser;

import ctp.thosttraderapi.CThostFtdcInputOrderActionField;
import ctp.thosttraderapi.CThostFtdcInputOrderField;
import ctp.thosttraderapi.CThostFtdcInvestorPositionField;
import ctp.thosttraderapi.CThostFtdcOrderField;
import ctp.thosttraderapi.CThostFtdcQryInvestorPositionField;
import ctp.thosttraderapi.CThostFtdcTradeField;

/**
 * 操作系统信息判断工具类
 * 
 * @author long.gf
 *
 */
public class TradeConvertUtils {
	
	
	public static QuoteVO initQuote(ReqVO  t) {
		QuoteVO q = new QuoteVO();
		q.setRequestId(t.getRequestId());
		q.setFid(t.getFid());
		q.setFtime(t.getFtime());
		q.setFp(t.getFp());
		q.setQuoteId(t.getQuoteId());
		q.setRefQuoteId(t.getRefQuoteId());
		q.setQty(t.getQty());
		q.setCost(t.getCost());
		q.setKpFlag(t.getKpFlag());
		q.setDirection(t.getDirection());
		q.setPloyNo(t.getPloyNo());
		q.setOrderStatus(t.getOrderStatus());
		return q;
	}
	
	public static TradeVO initTrade(QuoteVO  q) {
		TradeVO t = new TradeVO();
		t.setRequestId(q.getRequestId());
		t.setFid(q.getFid());
		t.setFtime(q.getFtime());
		t.setFp(q.getFp());
		t.setQuoteId(q.getQuoteId());
		t.setRefQuoteId(q.getRefQuoteId());
		t.setQty(q.getQty());
		t.setCost(q.getCost());
		t.setKpFlag(q.getKpFlag());
		t.setDirection(q.getDirection());
		t.setPloyNo(q.getPloyNo());
		return t;
	}
	
	public static ReqVO initQuoteReqVO(QuoteVO  q) {
		ReqVO t = new ReqVO();
		t.setRequestId(q.getRequestId());
		t.setFid(q.getFid());
		t.setFtime(q.getFtime());
		t.setFp(q.getFp());
		t.setQuoteId(q.getQuoteId());
		t.setRefQuoteId(q.getRefQuoteId());
		t.setQty(q.getQty());
		t.setCost(q.getCost());
		t.setKpFlag(q.getKpFlag());
		t.setDirection(q.getDirection());
		t.setPloyNo(q.getPloyNo());
		t.setOrderStatus(q.getOrderStatus());
		return t;
	}
	
	public static ReqVO initTradeReqVO(TradeVO  q) {
		ReqVO t = new ReqVO();
		t.setRequestId(q.getRequestId());
		t.setFid(q.getFid());
		t.setFtime(q.getFtime());
		t.setFp(q.getFp());
		t.setQuoteId(q.getQuoteId());
		t.setRefQuoteId(q.getRefQuoteId());
		t.setQty(q.getQty());
		t.setTradeId(q.getTradeId());
		t.setCost(q.getCost());
		t.setKpFlag(q.getKpFlag());
		t.setDirection(q.getDirection());
		t.setPloyNo(q.getPloyNo());
		return t;
	}
	 
	
	public static QuoteVO convertOrderRtn(CThostFtdcOrderField c) {
		int requestId = c.getRequestID();
		ReqVO  t  =  TradeUtils.REQUEST_MAP.get(requestId);
		QuoteVO q = initQuote(t);
		q.setQuoteId(c.getOrderSysID().trim());
		q.setRefQuoteId(c.getOrderRef().trim());
		q.setOrderStatus(String.valueOf(c.getOrderStatus()));
		return q;
	}
	
	public static TradeVO convertTradeRtn(CThostFtdcTradeField c) {
		String fid = c.getInstrumentID();
		String quoteId = c.getOrderSysID().trim();
		QuoteVO q = TradeUtils.ORDER_MAP.get(fid).get(quoteId);
		TradeVO t = initTrade(q);
		t.setQty(c.getVolume());
		t.setCost(c.getPrice());
		t.setTradeId(c.getTradeID().trim());
		return t;
	}
	
	public static BonsVO convertBonsRtn(CThostFtdcInvestorPositionField  c) {
		BonsVO t = new BonsVO();
		t.setQty(c.getPosition());
		t.setCost(c.getPositionCost());
		t.setFid(c.getInstrumentID());
		t.setDirection(c.getPosiDirection() == CtpTypeConstans.THOST_FTDC_PD_Long ? 1 : c.getPosiDirection() == CtpTypeConstans.THOST_FTDC_PD_Short ? -1 : 0 );
		return t;
	}
	
	
	public static CThostFtdcInputOrderField initOrderParams(QuoteVO  q,CtpUser ctpUser) {
		CThostFtdcInputOrderField  order = new CThostFtdcInputOrderField();
		order.setBrokerID(ctpUser.getBrokerId());
		order.setInvestorID(ctpUser.getInvestorId());
		order.setOrderPriceType(CtpTypeConstans.THOST_FTDC_OPT_LimitPrice);
		order.setContingentCondition(CtpTypeConstans.THOST_FTDC_CC_Immediately);
		order.setCombHedgeFlag(String.valueOf(CtpTypeConstans.THOST_FTDC_HF_Speculation));
		order.setTimeCondition(CtpTypeConstans.THOST_FTDC_TC_GFD);
		order.setVolumeCondition(CtpTypeConstans.THOST_FTDC_VC_AV);
		order.setForceCloseReason(CtpTypeConstans.THOST_FTDC_FCC_NotForceClose);
		
		order.setInstrumentID(q.getFid());
		order.setExchangeID(CtpTypeConstans.EXCHANGE_MAP.get(q.getFid()));
		order.setDirection(TradeUtils.isDF(q.getDirection()) ? CtpTypeConstans.THOST_FTDC_D_Buy : CtpTypeConstans.THOST_FTDC_D_Sell);
		order.setCombOffsetFlag(TradeUtils.isKC(q.getKpFlag()) ? String.valueOf(CtpTypeConstans.THOST_FTDC_OF_Open) : String.valueOf(CtpTypeConstans.THOST_FTDC_OF_Close));
		order.setLimitPrice(q.getCost());
		order.setVolumeTotalOriginal(q.getQty());
		order.setRequestID(q.getRequestId());
		return order;
	}
	
	public static CThostFtdcInputOrderActionField initOrderCancleParams(QuoteVO  q,CtpUser ctpUser) {
		CThostFtdcInputOrderActionField  field = new CThostFtdcInputOrderActionField();
		field.setBrokerID(ctpUser.getBrokerId());
		field.setInvestorID(ctpUser.getInvestorId());
		field.setInstrumentID(q.getFid());
		field.setExchangeID(CtpTypeConstans.EXCHANGE_MAP.get(q.getFid()));
		field.setActionFlag(CtpTypeConstans.THOST_FTDC_AF_Delete);
		field.setOrderSysID(q.getQuoteId());
		field.setRequestID(q.getRequestId());

		return field;
	}
	
	public static CThostFtdcQryInvestorPositionField initBonsParams(CtpUser ctpUser) {
		CThostFtdcQryInvestorPositionField field = new CThostFtdcQryInvestorPositionField();
		field.setBrokerID(ctpUser.getBrokerId());
		field.setInvestorID(ctpUser.getInvestorId());
		return field;
	}
}
